/*********************************************************************
*
*  Dynamic Variable and No Compression  Configuration Dialog Box.
*
**********************************************************************
* File Name:       AdvanceSettings.java
* Complier:        JAVA version "1.6.0_20 and onwards"
* IDE :            NetBeans
*/

/*
Copyright (C) 2012-2023, Microchip Technology Inc., and its subsidiaries. All rights reserved.

The software and documentation is provided by microchip and its contributors
"as is" and any express, implied or statutory warranties, including, but not
limited to, the implied warranties of merchantability, fitness for a particular
purpose and non-infringement of third party intellectual property rights are
disclaimed to the fullest extent permitted by law. In no event shall microchip
or its contributors be liable for any direct, indirect, incidental, special,
exemplary, or consequential damages (including, but not limited to, procurement
of substitute goods or services; loss of use, data, or profits; or business
interruption) however caused and on any theory of liability, whether in contract,
strict liability, or tort (including negligence or otherwise) arising in any way
out of the use of the software and documentation, even if advised of the
possibility of such damage.

Except as expressly permitted hereunder and subject to the applicable license terms
for any third-party software incorporated in the software and any applicable open
source software license terms, no license or other rights, whether express or
implied, are granted under any patent or other intellectual property rights of
Microchip or any third party.
*/

package MicrochipMPFS;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.Action;

public class AdvanceSettings extends javax.swing.JDialog {
    MainMPFS mainMpfs;
    ImageIcon icon = new ImageIcon(getClass().getResource("/Resource/mchpIcon.png"));
    public int reserveBlock=64;
    public String DynVarStr = "*.htm, *.html, *.cgi, *.xml";
    public String NoCompStr = "*.inc, snmp.bib";

    /** Creates new form AdvanceSettings */
    public AdvanceSettings(javax.swing.JFrame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        mainMpfs = (MainMPFS)parent;

        setIconImage(icon.getImage());
        defaultAdvanceSetting();
        toolTipAdvanceSettings();

        AdvanceSettingKeyEventActionIntialization();
        DynVarStr = txtDynFiles.getText();
        NoCompStr = txtDoNotCompress.getText();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        lblDynFiles = new javax.swing.JLabel();
        txtDynFiles = new javax.swing.JTextField();
        lblDoNotCompress = new javax.swing.JLabel();
        txtDoNotCompress = new javax.swing.JTextField();
        btnOK = new javax.swing.JButton();
        btnDefault = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("MPFS Processing Advance Settings");
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(180, 200));
        setModal(true);
        setResizable(false);

        lblDynFiles.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        lblDynFiles.setText("Dynamic Files:");

        txtDynFiles.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        txtDynFiles.setText("*.htm, *.html, *.cgi, *.xml");

        lblDoNotCompress.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        lblDoNotCompress.setText("Do Not Compress:");

        txtDoNotCompress.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        txtDoNotCompress.setText("*.inc, snmp.bib");

        btnOK.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnDefault.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        btnDefault.setText("Default");
        btnDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDefaultActionPerformed(evt);
            }
        });

        btnCancel.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(lblDynFiles, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnDefault, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(txtDynFiles, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblDoNotCompress, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(txtDoNotCompress, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblDynFiles, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(txtDynFiles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDoNotCompress, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDoNotCompress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOK)
                    .addComponent(btnCancel)
                    .addComponent(btnDefault, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {
        DynVarStr = txtDynFiles.getText();
        txtDynFiles.setText(DynVarStr);
        NoCompStr = txtDoNotCompress.getText();
        txtDoNotCompress.setText(NoCompStr);
        
        dispose();
    }

    private void btnDefaultActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:

        defaultAdvanceSetting();
        DynVarStr = "*.htm, *.html, *.cgi, *.xml";
        NoCompStr = "snmp.bib,*.bin";
        txtDynFiles.setText(DynVarStr);
        txtDoNotCompress.setText(NoCompStr);        
    }

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        txtDynFiles.setText(DynVarStr);
        txtDoNotCompress.setText(NoCompStr);

       dispose();
    }

    public void defaultAdvanceSetting()
    {
        txtDoNotCompress.setVisible(true);
        txtDynFiles.setVisible(true);
        lblDynFiles.setVisible(true);
        lblDoNotCompress.setVisible(true);
    }

    void commonFalse()
    {
        txtDoNotCompress.setVisible(false);
        txtDynFiles.setVisible(false);
        lblDynFiles.setVisible(false);
        lblDoNotCompress.setVisible(false);
        //lblTCPIP.setText("(Reserve block is only " +
            //"configured in TCPIPConfig.)");
        /*
        lblResvBlk.setVisible(false);
        txtResvBlk.setVisible(false);
        lblBytes.setVisible(false);
        */
    }
    void commontrue()
    {
        txtDoNotCompress.setVisible(true);
        txtDynFiles.setVisible(true);
        lblDynFiles.setVisible(true);
        lblDoNotCompress.setVisible(true);
        //lblTCPIP.setText("(Reserve block is only " +
            //"configured in TCPIPConfig.)");
        /*
        lblResvBlk.setVisible(true);
        txtResvBlk.setVisible(true);
        lblBytes.setVisible(true);
        */
    }

    public void setDynamicFileStr(String dynamicVarStr)
    {
        txtDynFiles.setText(dynamicVarStr);
        DynVarStr = txtDynFiles.getText();
    }

    public void setNoCompressionFileStr(String noCompressStr)
    {
        txtDoNotCompress.setText(noCompressStr);
        noCompressStr = txtDoNotCompress.getText();
    }

    public String getDynamicFileStr()
    {
        return txtDynFiles.getText();
    }

    public String getNoCompressionFileStr()
    {
        return txtDoNotCompress.getText();
    }

    void toolTipAdvanceSettings()
    {
        String infoString;

        /*
        infoString = "<html><body bgcolor=\"#FFFFCC\"><img src=\""+ getClass()
                     .getResource("/Resource/info.gif") + "\"> <b>";
        */

        infoString = "<html><body bgcolor=\"#FFFFCC\"> <b>";

        infoString = infoString + "<headings> <font size=\"4\">"
                +" Advanced Settings Help </font></headings></b><br>";

        /*
        radBtnMPFS2.setToolTipText(infoString +
                "Build an MPFS2 image that is compatible <br>with " +
                "the HTTP2 web server." + "</body></html>");

        radBtnMpfsCls.setToolTipText(infoString +
                "Build a classic MPFS image that is compatible <br>" +
                "with the original HTTP web server." + "</body></html>");

        txtResvBlk.setToolTipText(infoString +"The MPFS image will begin " +
                "at this address <br>in your external storage.  Memory befor" +
                    "e this address <br>will be reserved for your own " +
                    "application\'s use. <br>uploaded to external storage," +
                    "<br>such as EEPROM or serial Flash."+"</body></html>" );
        */

        txtDynFiles.setToolTipText(infoString + "These files will be searched " +
                "for<br>dynamic variables and indexed for <br>your applic" +
                "ation.  Enter file names or<br>extensions, separated by " +
                "commas. <br>for the remainder of the process."+"</body></html>");

        txtDoNotCompress.setToolTipText(infoString+"By default, MPFS2 will compress" +
                " any file <br>without dynamic variables.These files can <br> " +
                "be served to web browsers, but cannot be <br>" +
                "read by the device.Files that need to be <br>" +
                "accessed locally should not be compressed.<br>" +
                "Enter file names or extentions."+"</body></html>");
    }

    private void AdvanceSettingKeyEventActionIntialization()
    {
        Action  ESCactionListener = new AbstractAction () {
          public void actionPerformed(ActionEvent actionEvent) {
            setVisible(false);
            dispose();
          }
        };
        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true);
        JComponent comp = this.getRootPane();
        comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "ESCAPE");
        ActionMap actionMap = comp.getActionMap();
        actionMap.put("ESCAPE", ESCactionListener);

        /* OK button Action Solves MAC , Linux and Window enter key issue*/
        Action OKactionListener = new AbstractAction () {
          public void actionPerformed(ActionEvent actionEvent) {
            btnOKActionPerformed(actionEvent);
          }
        };
        /* Cancel button Action Solves MAC , Linux and Window enter key issue*/
        Action CancelactionListener = new AbstractAction () {
          public void actionPerformed(ActionEvent actionEvent) {
            btnCancelActionPerformed(actionEvent);
          }
        };
        /* Default button Action Solves MAC , Linux and Window enter key issue*/
        Action DefaultactionListener = new AbstractAction () {
          public void actionPerformed(ActionEvent actionEvent) {
            btnDefaultActionPerformed(actionEvent);
          }
        };
        KeyStroke enter_ok = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
        KeyStroke enter_default = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
        KeyStroke enter_cancel = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);

        btnOK.getInputMap(JButton.WHEN_FOCUSED).put(enter_ok, "OK");
        btnDefault.getInputMap(JButton.WHEN_FOCUSED).put(enter_default, "deafult");
        btnCancel.getInputMap(JButton.WHEN_FOCUSED).put(enter_cancel, "cancel");
        actionMap = btnOK.getActionMap();
        actionMap.put("OK", OKactionListener);
        btnOK.setActionMap(actionMap);

        actionMap = btnDefault.getActionMap();
        actionMap.put("deafult", DefaultactionListener);
        btnDefault.setActionMap(actionMap);

        actionMap = btnCancel.getActionMap();
        actionMap.put("cancel", CancelactionListener);
        btnCancel.setActionMap(actionMap);
    }

    /**
     * @param args the command line arguments
     */

    /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AdvanceSettings dialog = new AdvanceSettings(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDefault;
    private javax.swing.JButton btnOK;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel lblDoNotCompress;
    private javax.swing.JLabel lblDynFiles;
    private javax.swing.JTextField txtDoNotCompress;
    private javax.swing.JTextField txtDynFiles;
    // End of variables declaration//GEN-END:variables
}
